package cn.formaggie.service.impl;

import cn.formaggie.dao.OrderDao;
import cn.formaggie.dao.OrderItemDao;
import cn.formaggie.entity.model.order.Order;
import cn.formaggie.entity.model.order.OrderItem;
import cn.formaggie.service.face.OrderItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("orderItemService")
public class OrderItemServiceImpl implements OrderItemService {
    @Autowired
    private OrderItemDao orderItemDao;

    @Autowired
    private OrderDao orderDao;

    @Override
    public List<OrderItem> getAllOrderItems() {
        return orderItemDao.getAllOrderItems();
    }

    @Override
    public OrderItem getOrderItemById(Integer id) {
        return orderItemDao.getOrderItemById(id);
    }

    @Override
    public List<OrderItem> getOrderItemsByOrderId(Integer orderId) {
        return orderItemDao.getOrderItemsByOrderId(orderId);
    }

    @Override
    public List<OrderItem> findOrderItems(Map<String, Object> map) {
        return orderItemDao.findOrderItems(map);
    }

    @Override
    public Integer getOrderItemsCount(Map<String, Object> map) {
        return orderItemDao.getOrderItemsCount(map);
    }

    @Override
    public Boolean addOrderItem(OrderItem orderItem) {
        return orderItemDao.addOrderItem(orderItem)>0?true:false;
    }

    @Override
    public Boolean modifyOrderItemInfo(OrderItem orderItem) {
        return orderItemDao.modifyOrderItemInfo(orderItem)>0?true:false;
    }

    @Override
    public Boolean deleteOrderItem(Integer id) {
        return orderItemDao.deleteOrderItem(id)>0?true:false;
    }

    @Override
    public Boolean modifyState(OrderItem orderItem) {
        boolean issuccess=orderItemDao.modifyState(orderItem)>0?true:false;
        Order order=orderItemDao.getOrderItemById(orderItem.getId()).getOrder();
        List<OrderItem> items=orderItemDao.getOrderItemsByOrderId(order.getId());
        boolean isfinish=true,isout=true,ispay=true;
        for(OrderItem item:items){
            isfinish=isfinish&&item.getIsfinish();
            isout=isout&&item.getIsout();
            ispay=ispay&&item.getIspay();
        }
        order.setIsfinish(isfinish);
        order.setIsout(isout);
        order.setIspay(ispay);
        return issuccess&&(orderDao.modifyState(order)>0?true:false);
    }
}
